﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL.Dal;

public partial class SysAdmin_Index : System.Web.UI.Page
{
    public string LeftMenu = "";
    protected void Page_Load(object sender, EventArgs e)
    {

        //是否首次加载
        if (!IsPostBack)
        {
            GetUserMenu();
        }
    }

    /// <summary>
    /// 加载用户菜单
    /// </summary>
    public void GetUserMenu()
    {
        LeftMenu = "";

        SysMenuDAL MenuDal = new SysMenuDAL();

        DataTable dtUserTable = MenuDal.GetUserMenu();
        //生成左侧菜单的Json格式数据
        if (dtUserTable.Rows.Count > 0)
        {
            LeftMenu = "{";
            LeftMenu += "\"menus\":[";
            DataRow[] drRoots = dtUserTable.Select(" parentCode='Root'");
            if (drRoots.Length > 0)
            {
                string strTemp = "";
                for (int i = 0; i < drRoots.Length; i++)
                {
                    strTemp += "{";
                    strTemp += "\"menuid\":\"" + drRoots[i]["menuCode"] + "\",";
                    strTemp += "\"icon\":\"" + drRoots[i]["iconName"] + "\",";
                    strTemp += "\"menuname\":\"" + drRoots[i]["menuName"] + "\",";
                    strTemp += "\"menus\":[";

                    DataRow[] drChilds = dtUserTable.Select(" parentCode='" + drRoots[i]["menuCode"] + "'");
                    if (drChilds.Length > 0)
                    {
                        string strChildTemp = "";

                        for (int j = 0; j < drChilds.Length; j++)
                        {
                            strChildTemp += "{";
                            strChildTemp += "\"menuid\": \"" + drChilds[j]["menuCode"] + "\",";
                            strChildTemp += "\"menuname\": \"" + drChilds[j]["menuName"] + "\",";
                            strChildTemp += "\"icon\": \"" + drChilds[j]["iconName"] + "\",";
                            strChildTemp += "\"url\": \"" + drChilds[j]["url"] + "\"";
                            strChildTemp += "},";
                        }
                        strChildTemp = strChildTemp.TrimEnd(',');
                        strTemp += strChildTemp;
                    }
                    strTemp += "]},";
                }

                strTemp = strTemp.TrimEnd(',');
                //strTemp += "]"; ;
                LeftMenu += strTemp;
                LeftMenu += "]";
            }
            LeftMenu += "}";

        }
        else
        {
            //LeftMenu = "用户无系统权限";
        }

        userMenu.Value = LeftMenu;
    }
}